package com.example.fxblast

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.hzjq.core.Blast
import com.hzjq.core.bean.CapEntity
import com.hzjq.core.bean.ErrorResult
import com.hzjq.core.bean.LogBean
import com.hzjq.core.callback.OnScanCapCallback
import com.hzjq.core.receive.ReceiverInterceptor
import com.hzjq.core.util.BlastLog
import com.hzjq.fxlib.callback.ChargeCallback
import com.hzjq.fxlib.callback.OnChargeClickListener
import com.hzjq.fxlib.ui.RegisterCapActivity
import com.tbruyelle.rxpermissions2.RxPermissions
import io.reactivex.android.schedulers.AndroidSchedulers
import kotlinx.android.synthetic.main.activity_main.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@SuppressLint("CheckResult")
class MainActivity : AppCompatActivity() {


    private val caps = arrayListOf<CapEntity>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        EventBus.getDefault().register(this)

        Blast.getInstance().setDebug(true)

        Blast.getInstance().getChecker().start()

        Blast.getInstance().addInterceptor(object : ReceiverInterceptor {
            override fun onInterceptor(stateCode: Int): Boolean {
                val b = stateCode != 0
                BlastLog.e("stateCode:${stateCode}")
                if (b) {
                    Blast.getInstance().getChecker().stop()
                }
                return false
            }
        })

        mainOnlineScanCap.setOnClickListener {
            RxPermissions(this)
                .request(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe {
                    if (it) {
                        onScanCaps()
                    }
                }
        }
    }

    private fun onScanCaps(){
        caps.clear()
        Blast.getInstance().setMaxSupportCapCount(600)
        Blast.getInstance().scanCap().onScanCap(object : OnScanCapCallback{
            override fun notifyVoltageElectricChanged(mVoltage: Float, mElectric: Float) {

            }

            override fun onError(errorCode: ErrorResult) {
                mainCapProgressAction.text = errorCode.errorAction
            }

            override fun onProgressChanged(progress: Int, total: Int, action: String) {
                mainCapProgressBar.progress = progress
                mainCapProgressBar.max = total
                mainCapProgressAction.text = action
            }

            override fun onResult(t: MutableList<CapEntity>) {
                t.forEach {
                    it.password = "00000000"
                    it.rowNumber = caps.size
                    it.holeNumber = caps.size
                    caps.add(it)
                    if (it.isScanEnd) {
                        it.delay = 1000
                        toRegister()
                    }
                }
            }

            override fun onRetryCountChanged(retryCount: Int, action: String) {
                mainCapProgressAction.text = "${action}($retryCount)"
            }

        })
    }

    private fun toRegister() {
        RegisterCapActivity.start(this@MainActivity, caps, object : OnChargeClickListener {
            override fun onChargeClick(
                context: Context,
                charge: ChargeCallback
            ) {
                charge.go()
            }
        })
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onLogEvent(log: LogBean) {
        Log.e("log", log.content)
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

}